import { udp } from '@cscec/udp-ui';
import { defineAmisPlugin, getApiTpls } from '../../base/util';
import DetailPage from './fc';
import type { IProps } from './interface';

export default defineAmisPlugin<IProps>(({ amisEditor }) => {
  const { getSchemaTpl } = amisEditor;
  return {
    component: DetailPage,
    options: {
      type: 'detail-page',
      name: '明细模板',
      disabledRendererPlugin: true,
      order: -9998,
      icon: 'fa fa-tree',
      description: '表单明细，包含表单、多明细列表等',
      tags: ['页面模板'],
      scaffold: {
        toolbar: {
          type: 'tool-bar',
          buttonSize: 'middle',
          buttonType: 'primary',
          buttons: [
            { itemId: 'save', text: '保存', icon: 'SaveOutlined' },
            { itemType: 'divider' },
            { itemId: 'back', text: '返回', icon: 'ArrowLeftOutlined' }],
        },
        forms: [{
          type: 'form',
          id: 'form',
          name: 'form',
          title: '',
          wrapWithPanel: false,
          mode: 'horizontal',
          body: [
            {
              label: '文本框',
              type: 'input-text',
              name: 'cno',
            },
            {
              label: '文本框',
              type: 'input-text',
              name: 'cname',
            },
            {
              label: '文本框',
              type: 'input-text',
              name: 'remark',
            },
          ],
          actions: [],
        }],
        tabs: {
          type: 'tabs',
          removable: false,
          tabs: [
            {
              title: '表体',
              body: [{
                type: 'table',
                columns: [
                  {
                    label: 'ID',
                    name: 'id',
                    type: 'text',
                  },
                  {
                    type: 'text',
                    label: '列信息',
                    name: 'name1',
                  },
                ],
              }],
            },
          ],
        },
      },
      //区域的内容需要是数组，否则会被amis渲染一次后转换成数组
      regions: [
        {
          key: 'toolbar',
          label: '明细模板-工具条区域',
        },
        {
          key: 'forms',
          label: '明细模板-表单区区域',
        },
        {
          key: 'tabs',
          label: '明细模板-明细区tab区域',
        },
      ],
      panelBodyCreator(context: any) {
        return getSchemaTpl('tabs', getSchemaTpl('commonTpl', {
          scope: this,
          context,
          groups: [{
            title: '基本属性',
            body: [
              {
                type: 'input-text',
                name: 'busType',
                value: udp.getQueryValue('bizCode') || udp.getQueryValue('busType'),
                readOnly: true,
                required: true,
                label: '业务类型',
              },
            ],
          },
          getApiTpls('数据源', [
            {
              name: 'requestDetail',
              label: '获取详情数据',
              data: {
                code: 0,
                data: {
                  list: [],
                  pageNum: 1,
                  pageSize: 15,
                  total: 0,
                },
                message: '',
              },
            },
            {
              name: 'requestSave',
              label: '保存',
              data: {
                code: 0,
                data: true,
                message: '',
              },
            },
          ]),
        ],
        }));
      },
    },
  };
});
